﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using System.ComponentModel;

namespace Invini.Localization
{
    [TypeConverter(typeof(LocalizationStringTypeConverter))]
    [ContentProperty("Substrings")]
    public class LocalizationString : FrameworkElement
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LocalizationString), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure));
        public string Text
        {
            get { return (string)this.GetValue(TextProperty); }
            set { this.SetValue(TextProperty, value); }
        }

        public ObservableCollection<LocalizationString> Substrings
        {
            get;
            private set;
        }

        public LocalizationString()
        {
            this.Substrings = new ObservableCollection<LocalizationString>();
            this.Substrings.CollectionChanged += Substrings_CollectionChanged;
        }
        public LocalizationString(string text)
        {
            this.Substrings = new ObservableCollection<LocalizationString>();
            this.Substrings.CollectionChanged += Substrings_CollectionChanged;
            this.Text = text;
        }

        public static explicit operator LocalizationString(string text)
        {
            return new LocalizationString(text);
        }

        protected override Size MeasureOverride(Size availableSize) { return new Size(0, 0); }
        protected override Size ArrangeOverride(Size finalSize) { return new Size(0, 0); }
        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { }

        public override string ToString()
        {
            if (this.Text != null)
                return this.Text;

            if (this.Substrings.Count == 0)
                return "";

            StringBuilder result = new StringBuilder();
            foreach (LocalizationString substring in this.Substrings)
                result.Append(substring.ToString());

            return result.ToString();
        }

        protected override IEnumerator LogicalChildren
        {
            get
            {
                if (this.Substrings.Count == 0)
                    return null;

                return this.Substrings.GetEnumerator();
            }
        }

        private void Substrings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            INotifyCollectionChanged collection = sender as INotifyCollectionChanged;
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    if (e.NewItems != null)
                    {
                        foreach (object element in e.NewItems)
                            this.AddLogicalChild(element);
                    }
                    break;
                case NotifyCollectionChangedAction.Remove:
                    if (e.OldItems != null)
                    {
                        foreach (object element in e.OldItems)
                            this.RemoveLogicalChild(element);
                    }
                    break;
                case NotifyCollectionChangedAction.Reset:
                    if (e.OldItems != null)
                    {
                        foreach (object element in e.OldItems)
                            this.RemoveLogicalChild(element);
                    }
                    if (e.NewItems != null)
                    {
                        foreach (object element in e.NewItems)
                            this.AddLogicalChild(element);
                    }
                    break;
            }
        }
    }
}
